package com.qf.flash.ui.home;

import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.qf.flash.R;
import com.qf.flash.http.response.BusinessInfoBean;
import com.qf.flash.model.CommentBean;
import com.qf.flash.mvp.contract.BusinessContract;
import com.qf.flash.mvp.contract.BusinessDetContract;
import com.qf.flash.mvp.presenter.BCPresenter;
import com.qf.flash.mvp.presenter.BDPresenter;
import com.qf.flash.ui.home.adapter.BusinessTopItem;
import com.qf.flash.ui.home.adapter.GameCommentView;
import com.qf.flash.utils.AppToast;
import com.qf.flashlib.adapter.RVAdapter;
import com.qf.flashlib.base.BaseActivity;
import com.qf.flashlib.util.DensityUtil;
import com.qf.flashlib.widget.ViewUtil;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 创建日期：2020/3/8 on 22:28
 * 描述: 商家详情
 * 作者:戴旭东
 */
public class BusinessDetailActivity extends BaseActivity implements BusinessDetContract.View, BusinessContract.View {
    @BindView(R.id.tv_top_title)
    TextView tvTopTitle;
    @BindView(R.id.tv_sure)
    TextView tvBottomSure;
    @BindView(R.id.mRecyclerView)
    RecyclerView mRecyclerView;
    @BindView(R.id.mRefresh)
    SmartRefreshLayout mRefresh;
    @BindView(R.id.tv_top_right)
    TextView tvTopRight;

    private RVAdapter<Object> mAdapter;
    private BDPresenter mBDPresenter;
    private BCPresenter bcPresenter;

    private String shopId;
    private String emchatId;
    private String orderId;
    private int pageNo = 1;
    private boolean isCollect; //是否已收藏

    public static void start(Context activity, String orderId, String shopId, String emchatId, boolean showBottom) {
        Intent intent = new Intent(activity, BusinessDetailActivity.class);
        intent.putExtra("shopId", shopId);
        intent.putExtra("orderId", orderId);
        intent.putExtra("emchatId", emchatId);
        intent.putExtra("showBottom", showBottom);
        activity.startActivity(intent);
    }

    @Override
    protected void initInject() {
        mBDPresenter = new BDPresenter();
        bcPresenter = new BCPresenter();
        initPresenters(mBDPresenter, bcPresenter);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.item_business_detail;
    }

    @Override
    protected void initViewAndData() {
        initRecyclerView();
        tvTopTitle.setText("商家详情");
        shopId = getIntent().getStringExtra("shopId");
        orderId = getIntent().getStringExtra("orderId");
        emchatId = getIntent().getStringExtra("emchatId");
        boolean showBottom = getIntent().getBooleanExtra("showBottom", false);

        ViewUtil.setRightIcon(tvTopRight, R.mipmap.bar_collection);
        initRefresh();
        mBDPresenter.getBusinessDetail(shopId);
        loadCommentData();
        tvBottomSure.setVisibility(showBottom? View.VISIBLE: View.GONE);
    }

    private void initRefresh() {
        mRefresh.setEnableRefresh(false);
        mRefresh.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                pageNo++;
                loadCommentData();
            }
        });
    }

    private void loadCommentData() {
        mBDPresenter.getResponseList(shopId, pageNo);
    }

    private void initRecyclerView() {
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        GameCommentView gameCommentView = new GameCommentView();
        gameCommentView.setContentHorPadding(DensityUtil.dp2px(this, 14));
        mAdapter = new RVAdapter<>(new BusinessTopItem(this),gameCommentView);
        mRecyclerView.setAdapter(mAdapter);
    }


    @OnClick({R.id.tv_top_back, R.id.tv_sure,R.id.tv_top_right})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_top_back:
                finish();
                break;
            case R.id.tv_sure:
                bcPresenter.choseBusiness(orderId, shopId);
                break;

            case R.id.tv_top_right:
                if (isCollect) {
                    mBDPresenter.deleteCollectShop(shopId);
                } else {
                    mBDPresenter.collectShop(shopId);
                }
                break;
        }
    }

    @Override
    public void setBusinessData(BusinessInfoBean info) {
        List<Object> datas = mAdapter.getDatas();

        datas.add(0,info);
        mAdapter.setDatas(datas);
        isCollect = info.getIsCollect().equals("1");
        if(isCollect){
            ViewUtil.setRightIcon(tvTopRight, R.mipmap.icon_collectioned);
        }
    }

    @Override
    public void setCollected() {
        isCollect = true;
        AppToast.showToast("收藏完成！");
        ViewUtil.setRightIcon(tvTopRight, R.mipmap.icon_collectioned);
    }

    @Override
    public void setResponseList(List<CommentBean> commentBeans, int totalPage) {
        List<Object> datas = mAdapter.getDatas();
        datas.addAll(commentBeans);
        mAdapter.notifyDataSetChanged();
        mRefresh.setEnableLoadMore(pageNo < totalPage);
    }

    @Override
    public void cancelCollectSuccess() {
        isCollect = false;
        AppToast.showToast("收藏取消！");
        ViewUtil.setRightIcon(tvTopRight, R.mipmap.bar_collection);
    }

    @Override
    public void setBusinessData(List<BusinessInfoBean> businessInfoData, int totalPage) {

    }

    @Override
    public void choseSuccess() {
        finish();
        BusinessSuccessActivity.start(this, emchatId);
    }
}
